package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.mall.MallGoods;
import com.tencreat.domain.mall.dto.*;
import com.tencreat.domain.mall.vo.MallGoodTableVO;
import com.tencreat.domain.mall.vo.MallGoodsAllVO;
import com.tencreat.domain.mall.vo.MallStoreGoodsVO;
import com.tencreat.domain.mall.vo.MallSupplierGoodsVO;
import com.tencreat.service.mall.IMallGoodsService;
import com.tencreat.service.mall.IMallShopGoodsService;
import com.tencreat.service.mall.IMallSupplierGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品基本信息Controller
 *
 * @author tencreat
 * @date 2021-08-15
 */
@Api(tags="商品基本信息api")
@RestController
@RequestMapping("/mall/goods")
public class AdminMallGoodsController extends BaseController {

    @Autowired
    private IMallGoodsService mallGoodsService;


    @Autowired
    private IMallSupplierGoodsService supplierGoodsService;


    @Autowired
    private IMallShopGoodsService storeGoodsService;

    /**
     * 查询商品基本信息列表
     */
    @ApiOperation(value="查询商品基本信息列表——商品大厅")
    @PreAuthorize("@ss.hasPermi('mall:goods:list')")
    @PostMapping("/list")
    public HttpResult<IPage<MallGoodTableVO>> list(@RequestBody MallGoodsDTO mallGoods) {
        return HttpResult.ok(mallGoodsService.pageTableVo(mallGoods));
    }

    /**
     * 导出商品基本信息列表
     */
    @ApiOperation(value="导出商品基本信息列表")
    @PreAuthorize("@ss.hasPermi('mall:goods:export')")
    @Log(title = "商品管理>商品大厅", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(MallGoods mallGoods) {
        List<MallGoods> list = mallGoodsService.selectMallGoodsList(mallGoods);
        ExcelUtil<MallGoods> util = new ExcelUtil<MallGoods>(MallGoods.class);
        return util.exportExcel(list, "goods");
    }

    /**
     * 获取商品基本信息详细信息
     */
    @ApiOperation(value="获取商品基本信息详细信息")
    @PreAuthorize("@ss.hasPermi('mall:goods:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallGoods> getInfo(@RequestBody   ReqIdParam id) {
        return HttpResult.ok(mallGoodsService.selectMallGoodsById(id));
    }

    /**
     * 新增商品基本信息
     */
    @ApiOperation(value="新增商品基本信息")
    @PreAuthorize("@ss.hasPermi('mall:goods:add')")
    @Log(title = "商品管理>商品大厅", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody MallGoodsAddDTO mallGoodsAddVO) {
        if(mallGoodsAddVO.getNorms().size()<1){
            return HttpResult.error("请上传规格！！");
        }
        return to(mallGoodsService.insertMallGoodsPo(mallGoodsAddVO));
    }

    /**
     * 修改商品基本信息
     */
    @ApiOperation(value="修改商品基本信息")
    @PreAuthorize("@ss.hasPermi('mall:goods:edit')")
    @Log(title = "商品管理>商品大厅", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody MallGoods mallGoods) {
        return to(mallGoodsService.updateMallGoods(mallGoods));
    }

    /**
     * 删除商品基本信息
     */
    @ApiOperation(value="删除商品基本信息")
    @PreAuthorize("@ss.hasPermi('mall:goods:remove')")
    @Log(title = "商品管理>商品大厅", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody  ReqIdsParam ids) {
        return to(mallGoodsService.deleteMallGoodsByIds(ids));
    }


    @ApiOperation(value = "获取商品信息")
    @PreAuthorize("@ss.hasAnyPermi('mall:goods:query,mall:goods:edit')")
    @PostMapping("/getInfoGoodsVo")
    public HttpResult<MallGoodsAllVO> getInfoGoodsVo(@RequestBody ReqIdParam id){
        return HttpResult.ok(mallGoodsService.getInfoGoodsVo(id));
    }

    @ApiOperation(value = "更新商品信息")
    @PreAuthorize("@ss.hasAnyPermi('mall:goods:edit,mall:store-goods:edit')")
    @Log(title = "商品管理>商品大厅", businessType = BusinessType.UPDATE)
    @PostMapping("/updateGoodsPo")
    public HttpResult<Boolean> updateGoodsPo(@RequestBody MallGoodsAddDTO mallGoodsAddVO){
        mallGoodsAddVO.setUserId( getLoginCheck().getUser().getUserId());
        return to(mallGoodsService.updateMallGoodsPo(mallGoodsAddVO));
    }

    @ApiOperation(value = "最新商品")
    @PostMapping("/newGoods")
    public HttpResult<List<MallGoods>>getNewGoods(@RequestBody MallGoods mallGoods){
        return mallGoodsService.selectNewGoods(mallGoods);
    }


    /**
     * 修改商品基本信息
     */
    @ApiOperation(value="批量修改商品基本信息")
    @PreAuthorize("@ss.hasPermi('mall:goods:editList')")
    @Log(title = "商品管理>商品大厅", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/editList")
    public HttpResult<Boolean> editList(@RequestBody  List<MallGoods> mallGoods) {
        int k=0;
        for(int i=0;i<mallGoods.size();i++){
         k = mallGoodsService.updateMallGoods(mallGoods.get(i));
        }
         return to(k);
    }

    @ApiOperation("商品审核")
    @PreAuthorize("@ss.hasPermi('mall:goods:audit')")
    @PostMapping("/audit")
    @Log(title = "商品管理>商品大厅", businessType = BusinessType.AUDIT)
    public HttpResult audit(@RequestBody @Validated MallGoodsAuditDTO dto) {
        mallGoodsService.audit(dto);
        return HttpResult.ok();
    }

    @ApiOperation("供应商商品审核列表")
    @PreAuthorize("@ss.hasPermi('mall:goods:audit-list')")
    @PostMapping("/audit-list")
    HttpResult<Page<MallSupplierGoodsVO>> supplierList(@RequestBody @Validated MallSupplierGoodsAuditDTO dto) {
        dto.setAuditStatus(0);
        return HttpResult.ok(supplierGoodsService.listAudit(generatePage(dto), dto));
    }

    @ApiOperation("店铺商品审核列表")
    @PreAuthorize("@ss.hasPermi('mall:goods:audit-list')")
//    @PostMapping("/audit-store-list")
    HttpResult<Page<MallStoreGoodsVO>> storeList(@RequestBody @Validated MallStoreGoodsAuditDTO dto) {
        dto.setAuditStatus(0);
        return HttpResult.ok(storeGoodsService.listAudit(generatePage(dto), dto));
    }


    @ApiOperation("商品数量统计")
    @GetMapping("/queryCount")
    public HttpResult queryCount(String orderType) {
        return HttpResult.ok(mallGoodsService.queryCount(orderType));
    }

    /*****
     *  将当前商品的排序减1 顺序上升一位
     * @param mallGoods
     * @return
     */
    @PostMapping("/reSortOrderReduce")
    public HttpResult<Boolean> reSortOrderReduce(@RequestBody MallGoods mallGoods){
        return HttpResult.ok(mallGoodsService.reSortOrderReduce(mallGoods));
    }

    /*****
     * 将当前商品的排序加1 顺序下降一位
     * @param mallGoods
     * @return
     */
    @PostMapping("/reSortOrderPlus")
    public HttpResult<Boolean> reSortOrderPlus(@RequestBody MallGoods mallGoods){
        return HttpResult.ok(mallGoodsService.reSortOrderPlus(mallGoods));
    }


}
